package app

import (
	"chazhou/app/api/common"
	"chazhou/app/api/internal/controller"
	"fmt"
	"log"
	"net/http"
	"wmicro/server/httpserver"
	"wmicro/server/httpserver/registrycenter"
)

func Run() error {
	mux := controller.SetUp()

	// 服务注册中心
	consulRegisterCenter, err := registrycenter.NewConsulRegisterCenter()
	if err != nil {
		log.Printf("初始化服务注册中心失败: %s", err.Error())
		return err
	}

	server, err := httpserver.New(
		// 设置服务名称
		httpserver.WithServerOptionName(common.Config.App.Name),
		httpserver.WithServerHttpServer(
			&http.Server{
				Addr:    fmt.Sprintf(":%d", common.Config.App.Port),
				Handler: mux,
			},
		),
		// 设置注册中心
		httpserver.WithServerOptionServiceRegisterHost(common.Config.App.Host),
		httpserver.WithServerOptionServiceRegisterCenter(consulRegisterCenter),
	)
	if err != nil {
		log.Printf("httpServer创建失败: %s", err.Error())
		return err
	}

	// 运行
	return server.Run()
}
